//
//  ChangePwdViewController.m
//  ZhuKuang
//
//  Created by jayce on 17/7/12.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "SetPasswordApiModel.h"
#import "LoginViewController.h"

@interface ChangePwdViewController ()

@property (weak, nonatomic) IBOutlet UITextField *oldPwdTF;
@property (weak, nonatomic) IBOutlet UITextField *newerPwdTF;
@property (weak, nonatomic) IBOutlet UITextField *newerPwdAgainTF;
- (IBAction)back:(id)sender;
- (IBAction)confirmChange:(id)sender;

@end

@implementation ChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self p_hideNavigationBar];
}

- (void)p_hideNavigationBar {
    
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark - textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)confirmChange:(id)sender {
    
    //判断密码是否为空
    if (self.oldPwdTF.text.length==0){
        [MBProgressHUD postMessageWithText:@"旧密码不能为空!" View:kWindow];
        return;
    }
    if (self.newerPwdTF.text.length==0 || self.newerPwdAgainTF.text.length==0) {
        [MBProgressHUD postMessageWithText:@"新密码不能为空!" View:kWindow];
        return;
    }
    if (![self.newerPwdTF.text isEqualToString:self.newerPwdAgainTF.text]) {
        [MBProgressHUD postMessageWithText:@"请保持密码一致!" View:kWindow];
        return;
    }
    [self.view endEditing:YES];
    
    NSDictionary *postDic = @{
                              @"oldPassword" : _oldPwdTF.text,
                              @"newPassword" : _newerPwdTF.text,
                              @"personID" : [UserLoginModel getSessionID]
                              };
    [SetPasswordApiModel setPassword:postDic success:^(NSDictionary *response) {
        [MBProgressHUD postMessageWithText:[response objectForKey:@"msg"] View:kWindow];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            LoginViewController *login = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
            [self presentViewController:login animated:YES completion:nil];
        });
    }];
}
@end
